// pages/detail/detail.ts
const {submit} = require("../../api/move")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    dataList: [] as FormData[][],
    navName: [] as NavItem[],
    cateNumArray: [] as CateNameNum[],
    month: "" as string,
    year: "" as string,
    day: "" as string,
    week: "" as string,
    days: ['日', '一', '二', '三', '四', '五', '六'],
    isEdit: false as boolean,// 是否点击底部修改按钮
    isEditData: false as boolean ,//是否点击了修改数据
    showLoading: true as boolean,
    timer: null as number | null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const until = require("../../hooks/category")
    const channel = this.getOpenerEventChannel();
    channel.on("getDataList", ({ data, navName }: { data: FormData[][], navName: NavItem[] }) => {
      // 获取每个动作的数量
      const cateArray = until.classifyByNavId(data)
      // 遍历navName，进行整理
      this.setData({
        dataList: data,
        navName,
        cateNumArray: until.classifyNum(navName, cateArray),
        year: this.options.year,
        month: this.options.month,
        week: this.options.week,
        day: this.options.day
      })
      this.setData({
        showLoading: false
      })
    })
  },

  toActivity(): void {
    wx.navigateBack();
  },
  showEdit(): void {
    this.setData({
      isEdit: true
    })
  },
  commit(): void {
    const params = this.data.dataList.flat();
    const year = this.data.year;
    const month = this.data.month;
    const day = this.data.day
    const date = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`;    
    let data: BaseData[] = params.map(item => {
      // 对每一项添加 createDate 和 updateDate 属性
      return {
        ...item,
        createDate: date, // 使用当前日期作为 createDate
        updateDate: date, // 使用当前日期作为 updateDate
      };
    });
    submit(data).then((res:Response) => {
      if(res.code === 0) {
        wx.showToast({
          title:"提交成功",
          icon:"success"
        })
        this.data.timer = setTimeout(() => {
          wx.reLaunch({
            url:`/pages/calendar/calendar?year=${year}&month=${month}&day=${day}`
          })
        },1000)
      }
    })
  },
  edit(e: any): void {
    // 跳转到修改界面
    wx.navigateTo({
      url:"/pages/edit/edit",
      success: (res) => {
        res.eventChannel.emit("getDetailList", {
          data: e.currentTarget.dataset,
          date:{
            year:this.data.year,
            month: this.data.month,
            today: this.data.day,
            week: this.data.days[Number(this.data.week)]
          }
        })
      }
    })
  },
  delete(e: any): void {
    const {list:listIndex, group:groupIndex} = e.currentTarget.dataset
    let dataList = this.data.dataList;
    if (dataList[listIndex] && dataList[listIndex][groupIndex]) {
      dataList[listIndex][groupIndex].group -= 1;
      if( dataList[listIndex][groupIndex].group === 0) {
        dataList[listIndex].splice(groupIndex, 1);
      }
      if(!dataList[listIndex].length)  {
        dataList.splice(listIndex, 1);
      }
    }
    // 上面展示的数量也进行变化
    const until = require("../../hooks/category")
    const cateArray = until.classifyByNavId(dataList)
    const cateNumArray =  until.classifyNum(this.data.navName, cateArray)
    this.setData({
      dataList,
      cateNumArray
    });
  },
  // 点击结束
  overEdit(): void {
    this.setData({
      isEdit:false
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const dataList = this.data.dataList
    const until = require("../../hooks/category")
    const cateArray = until.classifyByNavId(dataList)
    const cateNumArray =  until.classifyNum(this.data.navName, cateArray)
    this.setData({
      dataList,
      cateNumArray,
    })
  },
  onUnload() {
    this.data.timer = null
  }
})

